from flask import Flask, flash, jsonify, Response, render_template, request, session, redirect, url_for
import simplejson as json
from flask_session import Session
import hashlib
from apps.admin import app, db
from apps.admin.util import common
from apps.admin.models.menu import Menu
from apps.admin.util.common import format_time


@app.route("/menus", methods=["GET"])
def menus():
    if request.headers.get('X-Requested-With') != 'XMLHttpRequest':
        return render_template("menu/index.html")
    # bk = Admin.query.get(1263)

    datas, total = Menu.getListByRole()
    data_list = []
    content = {}
    for data in datas:
        # print(data)
        createtime = format_time(data.createtime)
        updatetime = format_time(data.updatetime)
        content = {
            'id': data.id,
            'name': data.name,
            'authority': data.authority,
            'url': data.url,
            'parent_id': data.parent_id,
            'is_menu': data.is_menu,
            'sort': data.sort,
            'icon': data.icon,
            'createtime': createtime,
            'updatetime': updatetime,
        }
        data_list.append(content)
        content = {}

    result = {'code': 0, "msg": "", "count": total, "data": data_list}
    return Response(json.dumps(result), mimetype='application/json')


@app.route("/menu_edit", methods=["GET", "POST"])
def menu_edit():
    if request.method == 'GET':
        pass

    data = request.form
    data_id = data.get("id")
    name = data.get("name")
    is_menu = data.get("is_menu")  #
    url = data.get("url")  #
    authority = data.get("authority")  #
    icon = data.get("icon")  #
    sort = data.get("sort")  #
    parent_id = data.get("parent_id")  #

    if parent_id == '':
        parent_id = 0

    timestamp = time.time()
    menu = Menu.query.get(data_id)

    menu.name = name
    menu.is_menu = is_menu
    menu.url = url
    menu.authority = authority
    menu.icon = icon
    menu.sort = sort
    menu.parent_id = parent_id
    menu.updatetime = timestamp
    db.session.commit()  # 提交事务

    result = {'code': 200, "msg": "修改成功"}
    return Response(json.dumps(result), mimetype='application/json')


@app.route("/menu_add", methods=["GET", "POST"])
def menu_add():
    if request.method == 'GET':
        pass

    data = request.form
    name = data.get("name")
    authority = data.get("authority")  #
    is_menu = data.get("is_menu")  #
    url = data.get("url")  #
    icon = data.get("icon")  #
    sort = data.get("sort")  #
    parent_id = data.get("parent_id")  #

    if parent_id == '':
        parent_id = 0

    timestamp = time.time()

    menu = Menu(
        name=name,
        authority=authority,
        is_menu=is_menu,
        url=url,
        icon=icon,
        sort=sort,
        parent_id=parent_id,
        createtime=timestamp,
    )
    db.session.add(menu)
    result = db.session.commit()

    result = {'code': 200, "msg": "提交成功"}
    return Response(json.dumps(result), mimetype='application/json')


@app.route("/menu_del", methods=["POST"])
def menu_del():
    # user = User.query.filter_by(username='test').first()

    menu_id = request.form.get("id")

    menu = Menu.query.get(menu_id)
    db.session.delete(menu)
    db.session.commit()

    result = {'code': 200, "msg": "删除成功"}
    return Response(json.dumps(result), mimetype='application/json')
